namespace MC {
    export enum PlayerKey {
        mcData,
        txtr
    }
    /**动画id */
    export enum Key {
        HEAD = 0
    }
    /**资源配置 */
    export const MCRes = {
        [Key.HEAD]: ["headFrame_png", "headFrame_json"]
    };
    /**资源配置 key */

    export const MCKey = {
        [Key.HEAD]: "headFrame"
    };
    /**位置配置 使实际纹理图可处于父容器 零点 x y scale*/
    export const MCxy = {
        [Key.HEAD]: [-20, -19, 1]
    };

}
class MCMgr extends egret.EventDispatcher {
    public static _i: MCMgr;
    public static get i(): MCMgr {
        if (this._i == undefined) {
            this._i = new MCMgr();
        }
        return this._i;
    }
    static PLAY: string = "play";
    //缓存资源
    private MCdatas: any;
    //加载资源计数
    private count: any;
    //父节点
    private hosts: any;

    constructor() {
        super();
        this.clean();
        this.addEventListener(MCMgr.PLAY, this.playHandler, this);
    }

    public clean() {
        this.count = {};
        this.MCdatas = {};
        this.hosts = {};
    }

    public clearPlayer(id: MC.Key) {
        delete this.count[id];
        delete this.hosts[id];
        delete this.MCdatas[id];
    }

    private _loaderRes(id: MC.Key) {
        const [mcData, txtr] = MC.MCRes[id] as [string, string];
        this.MCdatas[id] = {};
        this.count[id] = 0;
        const _MCdata = this.MCdatas[id];

        RES.getResAsync(mcData, (data) => {
            _MCdata[MC.PlayerKey.mcData] = data;
            this.check(id);
        }, this);

        RES.getResAsync(txtr, (data) => {
            _MCdata[MC.PlayerKey.txtr] = data;
            this.check(id);
        }, this);

    }
    /**播放当前动画 */
    private playHandler(e: egret.Event) {
        const id: number = e.data;
        const mcData = this.MCdatas[id];
        const host: egret.DisplayObjectContainer = this.hosts[id];
        const frameName: string = MC.MCKey[id];
        const [x, y, scale] = MC.MCxy[id];
        if (host.numChildren > 0) {
            host.removeChildren();
        }
        /**播放 */
        var mcFactory: egret.MovieClipDataFactory = new egret.MovieClipDataFactory(mcData[MC.PlayerKey.txtr], mcData[MC.PlayerKey.mcData]);
        var mc1: egret.MovieClip = new egret.MovieClip(mcFactory.generateMovieClipData(frameName));
        host.addChild(mc1);
        mc1.gotoAndPlay(1, -1);
        mc1.x = x;
        mc1.y = y;
        mc1.scaleX = mc1.scaleY = scale;
        this.removeHost(id);
    }

    /**播放 */
    public play(id: MC.Key, host: any) {
        this.hosts[id] = host;
        const mcData = this.MCdatas[id];
        if (mcData != undefined) {
            this.dispatchEventWith(MCMgr.PLAY, false, id);
        } else {
            this._loaderRes(id);
        }
    }

    /**动画播放成功删除当前host */
    public removeHost(id: MC.Key) {
        delete this.hosts[id];
    }

    private check(id: MC.Key) {

        const keys = MC.MCRes[id];
        if (!keys) { return; }
        this.count[id]++;
        if (this.count[id] == keys.length) {
            this.dispatchEventWith(MCMgr.PLAY, false, id);
        }

    }

}

